package com.hunttown.mes.impl;

import com.hunttown.mes.common.utils.RegexpUtils;
import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportRemarkLogRpcService;
import com.hunttown.mes.rpc.domain.TransportRemarkLogDTO;
import com.hunttown.mes.rpc.domain.query.TransportRemarkLogDTOQuery;
import com.hunttown.mes.service.TransportRemarkLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.TransportRemarkLogConverts.convert;

/**
 * created by wangjunfu on 2019-05-19 01:20:14
 */
@Service
public class TransportRemarkLogRpcServiceImpl implements TransportRemarkLogRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportRemarkLogRpcServiceImpl.class);

    private final TransportRemarkLogService transportRemarkLogService;

    @Autowired
    public TransportRemarkLogRpcServiceImpl(TransportRemarkLogService transportRemarkLogService) {
        this.transportRemarkLogService = transportRemarkLogService;
    }

    @Override
    public TransportRemarkLogDTO insert(TransportRemarkLogDTO transportRemarkLogDTO) {
        try {
            return convert(transportRemarkLogService.insert(convert(transportRemarkLogDTO)));
        } catch (Exception e) {
            logger.error("TransportRemarkLog create failed.", e);
            throw new RuntimeException("TransportRemarkLog create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportRemarkLogDTO transportRemarkLogDTO) {
        try {
            return transportRemarkLogService.updateInfoById(convert(transportRemarkLogDTO));
        } catch (Exception e) {
            logger.error("TransportRemarkLog update by id failed.", e);
            throw new RuntimeException("TransportRemarkLog update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportRemarkLogDTOQuery transportRemarkLogDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportRemarkLogDTOQuery.getW_id() != null && transportRemarkLogDTOQuery.getW_id() > 0) {
                query.put("w_id", transportRemarkLogDTOQuery.getW_id());
            }

            if (transportRemarkLogDTOQuery.getIds() != null && !transportRemarkLogDTOQuery.getIds().equals("")) {
                query.put("ids", transportRemarkLogDTOQuery.getIds());
            }

            if (transportRemarkLogDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportRemarkLogDTOQuery.getStopFlag());
            }

            if (transportRemarkLogDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportRemarkLogDTOQuery.getDeleteFlag());
            }

            return transportRemarkLogService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportRemarkLog update by query failed.", e);
            throw new RuntimeException("TransportRemarkLog update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportRemarkLogDTO selectById(Integer id) {
        try {
            return convert(transportRemarkLogService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportRemarkLog select by id failed.", e);
            throw new RuntimeException("TransportRemarkLog select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportRemarkLogDTO selectObjByQuery(TransportRemarkLogDTOQuery transportRemarkLogDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportRemarkLogDTOQuery.getTableId() != null) {
                query.put("tableId", transportRemarkLogDTOQuery.getTableId());
            }

            if (transportRemarkLogDTOQuery.getRecordId() != null) {
                query.put("recordId", transportRemarkLogDTOQuery.getRecordId());
            }

            return convert(transportRemarkLogService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportRemarkLog select obj by query failed.", e);
            throw new RuntimeException("TransportRemarkLog select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportRemarkLogDTO> getListForPage(TransportRemarkLogDTOQuery transportRemarkLogDTOQuery) {
        Query query = Query.page(transportRemarkLogDTOQuery.getPn(), transportRemarkLogDTOQuery.getPageSize());

        if (transportRemarkLogDTOQuery.getId() != null && transportRemarkLogDTOQuery.getId() > 0) {
            query.put("id", transportRemarkLogDTOQuery.getId());
        }

        if (transportRemarkLogDTOQuery.getIds() != null && !transportRemarkLogDTOQuery.getIds().equals("")) {
            query.put("ids", transportRemarkLogDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(transportRemarkLogDTOQuery.getLikeName())) {
            query.put("likeName", transportRemarkLogDTOQuery.getLikeName());
        }

        if (transportRemarkLogDTOQuery.getTableId() != null && transportRemarkLogDTOQuery.getTableId() > 0) {
            query.put("tableId", transportRemarkLogDTOQuery.getTableId());
        }

        if (transportRemarkLogDTOQuery.getRecordId() != null) {
            // 判断是否是数字
            if (RegexpUtils.isInteger(transportRemarkLogDTOQuery.getRecordId().toString())) {
                query.put("recordId", transportRemarkLogDTOQuery.getRecordId());
            }
        }

        if (transportRemarkLogDTOQuery.getCreatePeople() != null && transportRemarkLogDTOQuery.getCreatePeople() > 0) {
            query.put("createPeople", transportRemarkLogDTOQuery.getCreatePeople());
        }

        if (StringUtils.isNotBlank(transportRemarkLogDTOQuery.getShowDate())) {
            query.put("showDate", transportRemarkLogDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportRemarkLogDTOQuery.getStartDate())) {
            query.put("startDate", transportRemarkLogDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportRemarkLogDTOQuery.getEndDate())) {
            query.put("endDate", transportRemarkLogDTOQuery.getEndDate());
        }

        if (StringUtils.isNotBlank(transportRemarkLogDTOQuery.getOrderBy())) {
            query.put("orderBy", transportRemarkLogDTOQuery.getOrderBy());
        }

        List<TransportRemarkLogDTO> list = convert(transportRemarkLogService.selectByQuery(query));
        Page<TransportRemarkLogDTO> page = Page.from(transportRemarkLogDTOQuery.getPn(), transportRemarkLogDTOQuery.getPageSize(), list);
        page.setTotalItems(transportRemarkLogService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportRemarkLogService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportRemarkLog delete failed.", e);
            throw new RuntimeException("TransportRemarkLog delete failed, Please order your code.", e);
        }
    }
}